package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itbuka.goods.domain.*;
import com.itbuka.goods.mapper.*;
import com.itbuka.goods.service.DetailService;
import com.itbuka.goods.mapper.GoodsProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * DetailServiceImpl 实现类
 */
@Service
public class DetailServiceImpl implements DetailService {

    @Autowired
    private GoodsProductMapper goodsProductMapper;

    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;

    @Autowired
    private GoodsProductDetailsMapper goodsProductDetailsMapper;

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;

    @Override
    public Detail findDetail(Long id) {
        Detail detail = new Detail();
        LambdaQueryWrapper<GoodsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsProduct::getId, id);
        lqw.eq(GoodsProduct::getIsDelete, 0);
        GoodsProduct goodsProduct = goodsProductMapper.selectOne(lqw);
        GoodsClassify classify = findClassify(goodsProduct.getClassifyId());
        StringBuilder classifyName = new StringBuilder();
        if (classify != null) {
            if (classify.getIsuperiorId() != null) {
                classifyName.append(classify.getClassifyName());
                // 如果有上级id，继续查询
                if (classify.getIsuperiorId() != 0) {
                    GoodsClassify classify1 = findClassify(classify.getIsuperiorId());
                    classifyName.insert(0, classify1.getClassifyName()+"==>>");
                    // 如果有上级id，继续查询
                    if (classify1.getIsuperiorId() != 0) {
                        GoodsClassify classify2 = findClassify(classify1.getIsuperiorId());
                        classifyName.insert(0, classify2.getClassifyName()+"==>>");
                    }
                }
            }
        }
        LambdaQueryWrapper<GoodsProductDetails> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(GoodsProductDetails::getProductId, goodsProduct.getId());
        lqw1.eq(GoodsProductDetails::getIsDelete, 0);
        List<GoodsProductDetails> list = goodsProductDetailsMapper.selectList(lqw1);
        for (GoodsProductDetails goodsProductDetails : list) {
            String specId = goodsProductDetails.getSpecId();
            String[] split = specId.split(",");
            StringBuilder name = new StringBuilder();
            int count = 0;
            for (String s : split) {
                GoodsSpec goodsSpec = goodsSpecMapper.selectById(Long.valueOf(s));
                if (count == 0) {
                    name.append(goodsSpec.getSpecValue());
                } else {
                    name.append(" ").append(goodsSpec.getSpecValue());
                }
                count++;
            }
            goodsProductDetails.setSpecId(name.toString());
        }
        //基本信息
        detail.setId(goodsProduct.getId().toString());
        detail.setCategory(classifyName.toString());
        detail.setName(goodsProduct.getName());
        detail.setSelling(goodsProduct.getSelling());
        detail.setParameter(goodsProduct.getParameter());
        //商品交易信息
        detail.setUnitMeasure(goodsProduct.getUnitMeasure());
        detail.setSalesModel(goodsProduct.getSalesModel());
        //商品规格及图片
        detail.setPrice(goodsProduct.getPrice().doubleValue());
        detail.setImg(goodsProduct.getImg());
        detail.setVideo(goodsProduct.getVideo());
        Map<Object, Object> map= new HashMap<>();
        map.put("spec",list);
        detail.setSpec(map);
        detail.setDescription(goodsProduct.getDescription());
        return detail;
    }

    //id查询分类对象
    public GoodsClassify findClassify(Long id) {
        LambdaQueryWrapper<GoodsClassify> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsClassify::getId, id);
        lqw.eq(GoodsClassify::getIsDelete, 0);
        return goodsClassifyMapper.selectOne(lqw);
    }
}